package config

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

var CONF_INSTANCE *ConfigInfo

// 配置信息
type ConfigInfo struct {
	// 监控配置
	Watchs       []WatchConfig `yaml:"watchs"`
	LogConf      `yaml:"log"`
	DingTalkConf `yaml:"dingtalk"`
	Profile      string `yaml:"profile"`
}

// 监控配置
type WatchConfig struct {
	// 应用名称
	AppName string `yaml:"appName"`
	// 启动脚本
	RestartScriptUrl string `yaml:"restartScriptUrl"`
	// 监听链接
	WatchUrls []string `yaml:"watchUrls"`
	// 最大失败次数
	MaxFail int `yaml:"maxFail"`
	// 检查任务的间隔
	Internal int64 `yaml:"internal"`
}

type LogConf struct {
	Path  string `yaml:"path"`
	Level string `yaml:"level"`
	Mode  string `yaml:"mode"`
}

type DingTalkConf struct {
	AccessToken string `yaml:"accessToken"`
}

func Setup(configFile string) {
	bs, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Fatal("读取配置文件异常")
	}
	err = yaml.Unmarshal(bs, &CONF_INSTANCE)
	if err != nil {
		log.Fatal("读取配置文件异常")
	}

}
